﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DAL;
using IBLL;
using MedicalSystem.Models;
using Microsoft.AspNetCore.Mvc;
using Model;

namespace MedicalSystem.Controllers
{
    public class RoomsController : Controller
    {
        MyDBContext db = new MyDBContext();
        private IRoomIntensiveBLL _Bll;
        public RoomsController(IRoomIntensiveBLL Bll)
        {
            _Bll = Bll;
        }

        public IActionResult GetRoom()
        {
            return View();
        }
        public IActionResult AddRoom()
        {
            return View();
        }
        public IActionResult UptRooms()
        {
            return View();
        }


        /// <summary>
        /// 病房显示查询
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        public List<RoomsView> GetRooms(string name="")
        {
            var list = (from a in db.RoomIntensive
                        join b in db.RoomType on a.RoomType_Id equals b.RoomType_Id
                        join c in db.Patient on a.Patient_Id equals c.Patient_Id
                        join d in db.Doctor on a.Doctor_Id equals d.Doctor_Id
                        select new RoomsView
                        {
                            RoomIntensive_Id=a.RoomIntensive_Id,
                            RoomIntensive_Number=a.RoomIntensive_Number,
                            RoomType_Name=b.RoomType_Name,
                            Patient_Name=c.Patient_Name,
                            RoomIntensive_TimeBegin=a.RoomIntensive_TimeBegin,
                            RoomIntensive_TimeEnd=a.RoomIntensive_TimeEnd,
                            Doctor_Name=d.Doctor_Name,
                            RoomIntensive_State=a.RoomIntensive_State
                        });
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(e => e.RoomIntensive_Number.Contains(name));
            }
            return list.ToList();
        }
        /// <summary>
        /// 病房类型下拉
        /// </summary>
        /// <returns></returns>
        public JsonResult GetRoomTypes()
        {
            return Json(_Bll.GetRoomTypes());
        }
        /// <summary>
        /// 添加病房
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddRooms(RoomIntensive m)
        {
            return Json(_Bll.AddRoom(m));
        }

        public JsonResult GetRoomById(int pid)
        {
            return Json(_Bll.GetRoomById(pid));
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public JsonResult UptRoom(RoomIntensive m)
        {
            return Json(_Bll.UptRoom(m));
        }
    }
}
